﻿using CollegeSim.CourseScheduler.Presentation.Facade;
using System;
using System.Windows.Forms;

namespace CollegeSim.CourseScheduler.Presentation
{
    // Author: Ryan Redburn
    // Project: Course Scheduler
    // Date: 2/16/2014

    public partial class FormCreateCourse : Form
    {
        #region [ Fields ]

        private readonly SchedulerFacade _facade;
        private readonly bool _isUpdate;
        private int _courseUpdateIndex;
        private int _courseUpdateId;

        #endregion

        #region [ Constructors ]

        // Initialize facade on instatiation
        // Determine whether form instance is used for updating or creation
        public FormCreateCourse(SchedulerFacade facade, bool isUpdate)
        {
            InitializeComponent();
            _facade = facade;
            _isUpdate = isUpdate;
        }

        #endregion

        #region [ Events ]

        // Prepare form resources
        private void frmCreateCourse_Load(object sender, EventArgs e)
        {
            // Populate instructor combo box
            cmbInstructor.DataSource = _facade.GetInstructorList();

            // End method if creation instance
            if (!_isUpdate) return;

            btnCreate.Text = "Update";

            // If udpate instance open course select form
            using (var frmCourseSelect = new FormCourseSelect(_facade))
            {
                frmCourseSelect.ShowDialog();

                // If the user selected a course, retrieve the index value
                if (frmCourseSelect.DialogResult == DialogResult.OK)
                {
                    _courseUpdateIndex = frmCourseSelect.SelectionIndex;
                }
                else
                {
                    Close();
                }
            }

            // Retrieve the selected course from the facade
            var courseInfo = _facade.GetCourseFromTempStore(_courseUpdateIndex);

            _courseUpdateId = int.Parse(courseInfo[0]);

            // Populate form fields with course information
            txtCRN.Text = courseInfo[1];
            txtSubject.Text = courseInfo[2];
            txtCourseNum.Text = courseInfo[3];
            txtTitle.Text = courseInfo[4];
            numCredits.Value = int.Parse(courseInfo[5]);
            numContact.Value = int.Parse(courseInfo[6]);
            chkOnline.Checked = (courseInfo[7] == "Yes");
        }

        // Passes data to the facade to start the course creation process
        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!_isUpdate) // Determine form instance type
                {
                    // Create course
                    _facade.CreateCourse(txtCRN.Text, txtSubject.Text, txtCourseNum.Text, txtTitle.Text,
                    (int)numCredits.Value, (int)numCredits.Value, chkOnline.Checked,
                    cmbInstructor.SelectedItem.ToString());
                }
                else
                {
                    // Update course
                    _facade.UpdateCourse(_courseUpdateId, txtCRN.Text, txtSubject.Text, txtCourseNum.Text,
                        txtTitle.Text, (int)numCredits.Value, (int)numCredits.Value, chkOnline.Checked,
                    cmbInstructor.SelectedItem.ToString());
                }
            }
            catch (ArgumentNullException x1)
            {
                MessageBox.Show(x1.Message);
            }
            catch (ArgumentOutOfRangeException x2)
            {
                MessageBox.Show(x2.Message);
            }
        }

        // Resets data fields
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtCRN.Clear();
            txtSubject.Clear();
            txtCourseNum.Clear();
            txtTitle.Clear();
            numCredits.ResetText();
            numContact.ResetText();
            chkOnline.Checked = false;
        }

        // Closes the form
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
